package com.rxjava.test.rxjava;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;

import java.net.URI;
import java.util.List;

public class ApiWrapper {
    Api api;

    public Observable<List<Cat>> queryCats(String query) {
        return Observable.create(new ObservableOnSubscribe<List<Cat>>() {
            @Override
            public void subscribe(ObservableEmitter<List<Cat>> emitter) throws Throwable {
                api.queryCats(query, new Api.CatsQueryCallback() {
                    @Override
                    public void onCatListReceived(List<Cat> cats) {
                        emitter.onNext(cats);
                    }

                    @Override
                    public void onError(Exception e) {
                        emitter.onError(e);
                    }
                });
            }
        });
    }

    public Observable<URI> store(Cat cat) {
        return Observable.create(new ObservableOnSubscribe<URI>() {
            @Override
            public void subscribe(ObservableEmitter<URI> emitter) throws Throwable {
                api.store(cat, new Api.StoreCallback() {
                    @Override
                    public void onCatStored(URI uri) {
                        emitter.onNext(uri);
                    }

                    @Override
                    public void onStoreFailed(Exception e) {
                        emitter.onError(e);
                    }
                });
            }
        });
    }
}
